<?php

require_once '../MMysql.php';

/**
 * Created by PhpStorm.
 * User: hcadmin
 * Date: 16/8/18
 * Time: 上午9:17
 */
class propertyfuction
{
    public function addagent($agent_name,$agent_tel,$agent_email,$agent_password)
    {

        $mysql = new MMysql();
        return $mysql->insert('agent',array(
            'agent_name'=>$agent_name,
            'agent_tel'=>$agent_tel,
            'agent_email'=>$agent_email,
            'agent_password'=>$agent_password,
            'del_flag'=>0,
            'create_time'=>date("y-m-d",time())
        ));
    }

    public function deleteProperty($property_id){

        $mysql = new MMysql();

        return $mysql->where(array('property_id'=>$property_id))->update('property',array(
            'del_flag'=>1,
        ));
    }

    public function updateagent($agent_name,$agent_tel,$agent_email,$agent_password,$agent_id)
    {
        $mysql = new MMysql();

        return $mysql->where(array('agent_id'=>$agent_id))->update('agent',array(
            'agent_name'=>$agent_name,
            'agent_tel'=>$agent_tel,
            'agent_email'=>$agent_email,
            'agent_password'=>$agent_password,
        ));
    }

    public function selectagent($page,$rows)
    {

        $mysql = new MMysql();
        $res = $mysql
            ->where(array("del_flag"=>0))
            ->limit($page,$rows)
            ->order(array('agent_id'=>'desc'))
            ->select('agent');

        $arraya=Array();
        foreach ($res as $u){

            $u["properties"]=$mysql->where(array('property_agent_id'=> $u["agent_id"],"del_flag"=>0))->count('property');

            $arraya[]=$u;
        }

        $count=$mysql->where(array("del_flag"=>0))->count('agent');

        $array=Array();
        $array["rows"]=$arraya;
        $array["total"]=$count;
        echo json_encode($array);
    }
}


$method =$_REQUEST["method"];

if($method=='list'){

    $d=new propertyfuction();

    return $d->selectagent($_REQUEST["page"],$_REQUEST["rows"]);

}
else if($method=='add'){
    $d=new propertyfuction();

//    echo $_REQUEST["parameter_value"];

    echo $d->addagent(
        $_REQUEST["agent_name"],$_REQUEST["agent_tel"],$_REQUEST["agent_email"],
        $_REQUEST["agent_password"]);
}

//else if($method=='delete'){
//    $d=new propertyfuction();
//    echo $d->deleteProperty($_REQUEST["property_id"]);
//}
else if($method=='update'){
    $d=new propertyfuction();
    echo $d->updateagent(
        $_REQUEST["agent_name"],$_REQUEST["agent_tel"],$_REQUEST["agent_email"],
        $_REQUEST["agent_password"],$_REQUEST["agent_id"]);
}
